--[[
滑轮滚动有问题，改为翻页更加丝滑
]]

class.save_ui1 = extends(class.panel) {
    reload = true,
    new = function (parent,x,y,w,h)
        local panel = class.save_ui1:builder 
        {
            parent = parent,
            x = x,
            y = y,
            w = w,
            h = h,
        }
        return panel
    end,
    init = function (self)
        class.panel.init(self)

        --[装备区域]
        local panel = self:add_panel('',20,20,self.w*0.4-30,self.h - 40)
        panel.back = panel:add_panel([[textures\black32.blp]],0,0,nil,320)
        panel:hide()
        self['装备区域'] = panel

        local w2,h2 = 64,64
        local useds = {
            {'武器',1,2},
            {'胸甲',3,2},
            {'头盔',2,1},
            {'护手',1,4},
            {'靴子',3,4},
            {'宠物',2,3},
        }
        self.useds = {}
        for a,da in ipairs(useds) do
            local name = da[1]
            local x = (da[2] - 1)*100 + 20
            local y = (da[3] - 1)*50 + 20
            local button = panel:add_sitemBtn(x,y,w2,h2)
            button.name = name
            button.title = button:add_text(name,-20,button.h,button.w+40,20,12,5)
            self.useds[a] = button
        end

        panel:add_namebutton('镶嵌宝石',70,panel.h-40,150,50)
        --[镶嵌界面]
        local panel = self:add_panel('',20,20,self.w*0.4-30,self.h - 40)
        panel.back = panel:add_panel([[textures\black32.blp]],0,0,nil,320)
        panel:hide()
        self['镶嵌界面'] = panel

        local types = {'装备槽','宝石槽'}
        local w,h = 96,96
        for a,name in ipairs(types) do
            local x = (a-1)*100 + 50
            local y = 50
            local button = panel:add_sitemBtn(x,y,w,h)
            button.name = name
            button.title = button:add_text(name,-20,button.h,button.w+40,20,12,5)
            self[name] = button
        end
        panel:add_namebutton('镶嵌',70,200,150,50)
        panel:add_namebutton('返回',70,panel.h-40,150,50)


        --[操作]

        --[仓库设置]
        local panel = self:add_panel([[textures\black32.blp]],self.w*0.40,20,self.w*0.6-20,self.h - 40)

        --仓库-菜单
        local menus = {'全部','护手','靴子','头盔','胸甲','武器','宠物','宝石'}
        local mw,mh = 80,40
        for a,name in ipairs(menus) do
            local x = ((a-1)%5) * (mw + 1) + 20
            local y = (math.ceil(a/5) - 1) * (mh + 5)
            local button = panel:add_namebutton(name,x,y,mw,mh)
            button.search = name
            if a==1 then
                ac.wait(100,function()
                    self:on_button_clicked(button)
                end)
            end
        end
                
        --仓库-按钮
        local item_panel = panel:add_panel('',0,100,panel.w - 30,panel.h - 70)
        local w,h = 64,64
        self.buttons = {}
        for a=1,6 do
            for b=1,6 do
                local x = (b-1)*(w + 4) + 30
                local y = (a-1)*(h + 4)
                local button = item_panel:add_sitemBtn(x,y,w,h)
                button.index = #self.buttons + 1
                table.insert(self.buttons,button)
            end
        end
        
        --[翻页]
        local w0,h0 = 21,26
        local w1 = 80
        local x0 = (panel.w - 2*w0 - w1)/2
        local y0 = panel.h - 30
        local art1 = [[ui\上一页按钮.tga]]
        local art2 = [[ui\下一页按钮.tga]]
        self.page_los = panel:add_button(art1,x0,y0,w0,h0)
        self.page_add = panel:add_button(art2,x0+w0+w1,y0,w0,h0)
        self.page_text = panel:add_text('1/1',x0+w0,y0,w1,h0,10,5)
        self.page = 1
        self.max_page = 1

        return self
    end,
    --[模式]
    set_mode = function(self,mode)
        self.mode = mode
        if mode=='展示' then
            self['装备区域']:show()
            self['镶嵌界面']:hide()
        elseif mode=='镶嵌' then
            self['装备区域']:hide()
            self['镶嵌界面']:show()
        end
    end,
    --[更新]
    update = function(self,flag)
        if flag then
            self.page = 1
            self:set_mode('展示')
        end
        local player = ac.player.self
        local search = self.search or '全部'
        local data = player:get_data('存档装备-显示') or {}
        local used = player:get_data('存档装备-携带') or {}
        local list = data[search] or {}
        local index = (self.page - 1) * #self.buttons
        for a,button in ipairs(self.buttons) do
            local skill = list[a + index]
            button:set_data(skill)
        end

        --装备界面
        for a,button in ipairs(self.useds) do
            local skill = used[button.name]
            button:set_data(skill)
        end

        --镶嵌
        local skill1 = player:get_data('选择-镶嵌装备')
        local skill2 = player:get_data('选择-镶嵌宝石')
        self['装备槽']:set_data(skill1)
        self['宝石槽']:set_data(skill2)

    end,
        
    --[点击]
    on_button_clicked = function(self,button)
        local search = button.search
        if search then
            if self.last_search then
                self.last_search.text:set_color(0xffffffff)
            end
            self.search = search
            self.last_search = button
            self.last_search.text:set_color(0xff00ff00)
            self:update()
        end
        if button.name=='镶嵌宝石' then
            self:set_mode('镶嵌')
        end
        if button.name=='返回' then
            self:set_mode('展示')
        end
        if button.name=='镶嵌' then
            ac.sync('同步-开始镶嵌宝石')
        end
    end,

    --[进入]
    on_button_mouse_enter = function(self,button)
        local skill = button.skill
        if skill then
            ac.game:notify('存档装备-显示文本',button,skill)
        end
    end,

    --[离开]
    on_button_mouse_leave = function(self,button)
        ac.game:notify('存档装备-隐藏文本',button)
    end,

    --[右击装备]
    on_button_right_clicked = function(self,button)
        local skill = button.skill
        if skill then
            if self.mode=='展示' then
                ac.sync('同步-佩戴存档装备',skill.id)
            elseif self.mode=='镶嵌' then
                if button.name=='装备槽' then
                    ac.sync('同步-设置镶嵌参数','选择-镶嵌装备',skill.id)
                elseif button.name=='宝石槽' then
                    ac.sync('同步-设置镶嵌参数','选择-镶嵌宝石',skill.id)
                end
            end
        end
    end,

    --[选择]

}

-- local w,h = 1100,600
-- local ui = class.save_ui1:builder 
-- {
--     x = (1920 - w)/2,
--     y = 150,
--     w = w,
--     h = h,
--     show_flag = false,
--     level = 99,
-- }
